package com.sdry.web.controller.lz;

import com.sdry.model.lz.DeviceAccount;
import com.sdry.model.lz.LzQueryCriteria;
import com.sdry.service.lz.DeviceAccountService;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

/**
 * @ClassName DeviceAccountController
 * @Description 设备台帐
 * @Author lz
 * @Date 2018年9月25日 15:44:46
 * @Version 1.0
 */
@Controller
@RequestMapping("/deviceAccount")
public class DeviceAccountController {

    public static final Integer AFFECT_ROW = 0;
    @Resource
    DeviceAccountService deviceAccountService;

    /**
     * 新增设备台帐信息
     * @param response
     * @param deviceAccount 设备台帐实体
     */
    @RequestMapping("/addDeviceAccount")
    public void addDeviceAccount(HttpServletResponse response, DeviceAccount deviceAccount) throws Exception {
        //String deviceNo = request.getParameter("deviceNo");
        //String deviceName = request.getParameter("deviceName");
    	if (deviceAccount != null) {
			if(StringUtil.isEmpty(deviceAccount.getBuyTime())){
				deviceAccount.setBuyTime(null);
			}
			if(StringUtil.isEmpty(deviceAccount.getUseTime())){
				deviceAccount.setUseTime(null);
			}
		}
        Long affectRow = deviceAccountService.addDeviceAccount(deviceAccount);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 单个删除和批量删除
     * @param response
     * @param idArr id数组
     * @throws Exception
     */
    @RequestMapping("/deleteDeviceAccountById")
    public void deleteDeviceAccountById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr) throws Exception {
        for (int i = 0; i < idArr.length; i++) {
            Long affectRow = deviceAccountService.deleteDeviceAccountById(Long.parseLong(idArr[i]));
            if (affectRow > AFFECT_ROW) {
                ResponseUtil.write(response, affectRow);
            } else {
                ResponseUtil.write(response, AFFECT_ROW);
            }
        }
    }

    /**
     * 根据ID修改设备信息
     * @param response
     * @param deviceAccount 选中行的设备
     * @throws Exception
     */
    @RequestMapping("/editDeviceAccountById")
    public void editDeviceAccountById(HttpServletResponse response, DeviceAccount deviceAccount) throws Exception {
        if (deviceAccount != null) {
            if(StringUtil.isEmpty(deviceAccount.getBuyTime())){
                deviceAccount.setBuyTime(null);
            }
            if(StringUtil.isEmpty(deviceAccount.getUseTime())){
                deviceAccount.setUseTime(null);
            }
        }
        Long affectRow = deviceAccountService.editDeviceAccountById(deviceAccount);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 分页查询设备信息
     * @param response
     * @param criteria 分页查询条件
     */
    @RequestMapping("/queryDeviceAccount")
    public void queryDeviceAccount(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<DeviceAccount> deviceList = deviceAccountService.queryDeviceAccount(criteria);
        int count = deviceAccountService.countDeviceAccount(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(deviceList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
